package com.atguigu.guli.service.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.*;
import com.atguigu.guli.service.base.handler.GuliException;
import com.atguigu.guli.service.base.result.ResultCodeEnum;
import com.atguigu.guli.service.vod.service.VodService;
import com.atguigu.guli.service.vod.util.VodProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
@Slf4j
public class VodServiceImpl implements VodService {
    @Autowired
    private VodProperties vodProperties ;
    @Override
    public String uploadVideo(MultipartFile file) {
        UploadVideoImpl uploader = new UploadVideoImpl();//客户端对象
        //fileName代表上传文件的名称：vod会用来截取文件扩展名 拼接title
        //请求对象
        UploadStreamRequest request = null;
        try {
            request = new UploadStreamRequest(vodProperties.getAccessKeyId() ,
                    vodProperties.getAccessKeySecret(), file.getOriginalFilename() ,file.getOriginalFilename()
                    , file.getInputStream());
        } catch (Exception e) {
            throw new GuliException(ResultCodeEnum.VIDEO_UPLOAD_TOMCAT_ERROR , e);
        }
        //请求对象中可以配置上传视频如何处理：
//        request.setCoverURL(); 设置视频封面地址
//        request.setTemplateGroupId(); 设置模板组id
        request.setWorkflowId(vodProperties.getWorkflowId());//设置工作流id

        UploadStreamResponse response = uploader.uploadStream(request);//客户端发起请求
        if(response.isSuccess()){
            return response.getVideoId();
        }else{
            log.error("上传失败的错误码："+response.getVideoId());
            log.error("错误信息： "+response.getMessage());
            throw new GuliException(ResultCodeEnum.VIDEO_UPLOAD_ALIYUN_ERROR);
        }
    }

    @Override
    public void deleteVideo(String videoId) {
        try {
            DefaultProfile profile = DefaultProfile.getProfile(vodProperties.getRegionId(),
                    vodProperties.getAccessKeyId(), vodProperties.getAccessKeySecret());
            DefaultAcsClient client = new DefaultAcsClient(profile);
            DeleteVideoRequest request = new DeleteVideoRequest();//请求对象
            //支持传入多个视频ID，多个用逗号分隔
            request.setVideoIds(videoId);//传入要删除的视频id
            DeleteVideoResponse response = client.getAcsResponse(request);//使用客户端发起请求  并返回响应结果
        } catch (Exception e) {
            throw new GuliException(ResultCodeEnum.VIDEO_DELETE_ALIYUN_ERROR , e);
        }
    }

    @Override
    public String getPlayUrl(String videoId) {
        try {
            DefaultProfile profile = DefaultProfile.getProfile(vodProperties.getRegionId(),
                    vodProperties.getAccessKeyId(), vodProperties.getAccessKeySecret());
            DefaultAcsClient client = new DefaultAcsClient(profile);
            GetPlayInfoRequest request = new GetPlayInfoRequest();//创建请求对象
            //https://outin-a6105734031411eba24000163e024c6a.oss-cn-shanghai.aliyuncs.com/sv/56015e70-17d84727442/56015e70-17d84727442.mp4?Expires=1638761956&OSSAccessKeyId=LTAIwkKSLcUfI2u4&Signature=y1hvXAR%2B4vq2qiAZzEOmXfoVczA%3D
            request.setVideoId(videoId);//传入未加密视频的id
            GetPlayInfoResponse response = client.getAcsResponse(request);//客户端发起请求得到相应
            List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
            //播放地址
            return playInfoList.get(0).getPlayURL();
        } catch (Exception e) {
            throw new GuliException(ResultCodeEnum.FETCH_PLAYAUTH_ERROR , e);
        }
    }

    @Override
    public String getPlayAuth(String videoId) {
        try {
            DefaultProfile profile = DefaultProfile.getProfile(vodProperties.getRegionId(),
                    vodProperties.getAccessKeyId(), vodProperties.getAccessKeySecret());
            DefaultAcsClient client = new DefaultAcsClient(profile);
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            //通过请求对象配置凭证过期时间
            request.setAuthInfoTimeout(2000L);
            request.setVideoId(videoId);
            GetVideoPlayAuthResponse response  = client.getAcsResponse(request);
            return response.getPlayAuth();
        } catch (Exception e) {
            throw new GuliException(ResultCodeEnum.FETCH_PLAYAUTH_ERROR , e);
        }
    }
}
